import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:38917
 * Date:2025/4/11
 * Time:18:26
 */
public class Solution {
//    public void sortColors(int[] nums) {
//        int[] count = new int[3];
//        for (int num : nums) {
//            count[num]++;
//        }
//        int index = 0;
//        for (int i = 0; i < count.length; i++) {
//            int cur = count[i];
//            while (cur != 0){
//                nums[index] = i;
//                index++;
//                cur--;
//            }
//        }
//    }
//    public static void main(String[] args) {
//        Solution solution = new Solution();
//        int[] nums = new int[]{2, 0, 2, 1, 1, 0};
//        solution.sortColors(nums);
//        System.out.println(Arrays.toString(nums));
//    }
    public void sortColors(int[] nums) {
        int length = nums.length;
        if (length == 1) return;
        int left = -1,index = 0,right = length;
        while (index < right) {
            if (nums[index] == 0) {
                sort(++left,index,nums);
                index++;
            }else if (nums[index] == 1) {
                index++;
            }else {
                sort(--right,index,nums);
            }
        }
    }
    public void sort(int x,int y,int[] nums){
        int temp = nums[x];
        nums[x] = nums[y];
        nums[y] = temp;
    }
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {0, 1, 2, 2, 1, 0, 0, 0, 1};
        solution.sortColors(nums);
        System.out.println(Arrays.toString(nums));
    }
}
